# coding = UTF-8
# @Time : 2022/05/10 19:01
# @Author : PP_YY
# @File : 实验3.py
# @Description : 计算两个向量余弦值
import tkinter
import numpy


top = tkinter.Tk()
top.title("余弦相似度计算")
top.geometry('500x300')
top.resizable(0, 0)
entry_string1 = tkinter.Entry(top, bd=1,width=50)
entry_string2 = tkinter.Entry(top, bd=1,width=50)
show_data = tkinter.Text(top,bd=0,font=('等线', 10),highlightthickness=0,state="disable",width=56,height=5)


def entry_to_list(str_entry):
	list_out = str_entry.split(',')
	print(list_out)
	return list(map(float,list_out))


def calculate_cos_similar(A,B):
	if len(A) != len(B):
		return 2
	if A == B:
		return 1
	up = 0
	downA = 0
	downB = 0
	for i in range(len(A)):
		up += A[i] * B[i]
	for i in range(len(A)):
		downA += A[i] ** 2
	for i in range(len(A)):
		downB += B[i] ** 2
	downA = downA ** 0.5
	downB = downB ** 0.5

	print(up)
	print(downA)
	print(downB)

	return up/(downA*downB)


def main():
	global show_data,entry_string2,entry_string1
	show_text = ''
	A = entry_to_list(entry_string1.get())
	B = entry_to_list(entry_string2.get())
	print(A)
	print(B)
	cos = round(calculate_cos_similar(A,B),2)

	if cos == 2:
		show_text = show_text + "输入的数据不对！"
	else:
		show_text = show_text + str(cos)

	show_data.config(state="normal")
	show_data.delete('1.0','end')
	show_data.insert('1.0',show_text)


def GUI_main():
	global entry_string1,top,show_data,entry_string2
	notice_label = tkinter.Label(top, text="请输入两个向量,用\",\"隔开", width=40, font=('等线', 12))
	start_button = tkinter.Button(top, text="计算", command=main, bd=1, font=('等线', 12))
	notice_label1 = tkinter.Label(top,text="余弦相似度为:",font=('等线', 8))
	entry_string1.place(x=15, y=40)
	entry_string2.place(x=15, y=70)
	start_button.place(x=380, y=50)
	notice_label.place(x=-80, y=10)
	notice_label1.place(x=15,y=100)
	#show_data.insert('1.0',"data\n")
	show_data.place(x=15,y=120)
	top.mainloop()


if __name__ == "__main__":
	GUI_main()



